package com.cool.service.sysmanage.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.bo.sysmanage.common.PageQueryBo;
import com.cool.common.CommonResult;
import com.cool.mapper.sysmanagemapper.MenuPoMapper;
import com.cool.po.sysmanagepo.MenuPo;
import com.cool.po.sysmanagepo.RolePo;
import com.cool.po.sysmanagepo.StaffPo;
import com.cool.service.sysmanage.DdMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 菜单表(DdMenu)表服务实现类
 *
 * @author Jungle
 * @since 2020-07-28 16:47:19
 */
@Service("ddMenuService")
@Transactional(rollbackFor = Exception.class)
public class DdMenuServiceImpl implements DdMenuService {
    @Resource
    private MenuPoMapper menuPoMapper;

    @Override
    public List<MenuPo> selectTreeList() {
        return menuPoMapper.selectTreeList();
    }

    @Override
    public Integer insert(MenuPo menuPo) {
        if(StringUtils.isEmpty(menuPo.getMenuTitle()) || StringUtils.isEmpty(menuPo.getMenuName())){
            return -1;
        }

        //查询父级菜单的level
        if(menuPo.getMenuParentId() == 0){
            menuPo.setMenuLevel(0);

        }else {
            Integer level = menuPoMapper.selectLevelByParentId(menuPo.getMenuParentId());

            menuPo.setMenuLevel(++level);
        }

        DateTime date = DateUtil.date();
        menuPo.setMenuCreate(date);
        menuPo.setMenuUpdate(date);
        return menuPoMapper.insertSelective(menuPo);
    }

    @Override
    public MenuPo selectById(Integer id) {
        return menuPoMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateById(Integer id, MenuPo menuPo) {

        menuPo.setMenuCreate(null);
        menuPo.setMenuUpdate(DateUtil.date());
        return menuPoMapper.updateByPrimaryKeySelective(menuPo);
    }

    @Override
    public Integer deleteById(Integer id) {
        return menuPoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateState(Integer id, Integer state) {
        return menuPoMapper.updateStateById(id,state);
    }

    @Override
    public PageQueryBo pagingQuery(Integer menuParentId, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<MenuPo> list = menuPoMapper.selectByMenuParentId(menuParentId);

        PageInfo<MenuPo> staffPoPageInfo = new PageInfo<>(list);

        PageQueryBo pageQueryBo = new PageQueryBo(
                staffPoPageInfo.getPageSize(),
                staffPoPageInfo.getPageNum(),
                staffPoPageInfo.getPages(),
                staffPoPageInfo.getTotal(),
                staffPoPageInfo.getList()
        );

        return pageQueryBo;
    }


}